<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\admin\AccountModel;
use app\common\model\index\UserModel;
use app\common\model\paths\PlatformModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\service\AdminService;

/**
 * 会员管理
 *
 * @package app\admin\controller
 * @Description 会员管理
 * @author wangqinghui
 * @date 2024-10-19 16:05
 */
class Member extends Controller
{
    /**
     * 列表
     *
     * @Description 列表
     * @auth true
     * @menu true
     * @author wangqinghui
     * @date 2024-10-19 16:17
     * @return void
     */
    public function index()
    {
        UserModel::mQuery()->layTable(function () {
            $this->title = '会员管理';
            // 获取下级数据
            //$this->checkAuth(AdminService::getPlatformId());
        }, function (QueryHelper $query) {
            $query->where('is_deleted', 0);

            $this->check_account = false;
            $this->check_user    = false;

            if (!$this->super) {
                $query->where('job_place', $this->account_id);
            }
            $query->like('account,true_name,nick_name')->equal('mobile,email,job_number,job_place')->dateBetween('create_at');
            $query->order('id asc');
        });
    }

    /**
     * _index_page_filter
     *
     * @Description 格式化列表数据
     * @author wangqinghui
     * @date 2024-10-19 16:18
     * @param array $data
     * @return void
     */
    protected function _index_page_filter(array &$data)
    {
        if(!empty($data)){
            // 获取平台数据
            $platform_ids = array_column($data, 'job_place');
            $platform_data = [];
            if(!empty($platform_ids)){
                $platform_data = AccountModel::mk()->where('id', 'in', $platform_ids)->column('title', 'id');
            }
            // 获取性别
            $sexArr = UserModel::getSexArr();
            // 获取学历
            $backgroundArr = UserModel::getBackgroundArr();
            foreach ($data as $key => $value) {
                $data[$key]['platform_text'] =  !empty($platform_data[$value['job_place']]) ? $platform_data[$value['job_place']] : '';
                $data[$key]['sex_text'] = !empty($sexArr[$value['sex']]) ? $sexArr[$value['sex']] : '';
                $data[$key]['edu_background_text'] = !empty($backgroundArr[$value['edu_background']]) ? $backgroundArr[$value['edu_background']] : '';
            }
        }
    }

    /**
     * 删除用户
     *
     * @Description 删除用户
     * @auth true
     * @author wangqinghui
     * @date 2024-10-19 16:17
     * @return void
     */
    public function remove ()
    {
        $id = $this->request->post('id', 0);
        if(empty($id) || ($id <= 0)){
            $this->error('参数错误');
        }
        UserModel::mDelete('id', [
            'id'    => $id
        ]);
    }
}